package com.guagua.chat.photo;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.guagua.base.database.ConnectionManager;
import com.guagua.chat.base.BaseDao;
/** ******************************************************************
* 类概要:图片操作类，对表t_photo的操作
* @author 郑国英
* @version 2012-3-20 下午8:00:00
*******************************************************************/

public class PhotoDao extends BaseDao {
	/**
	 * 函数说明：根据图片id查询图片基本信息 <br/>
	 * 
	 * @param photo_id
	 *            类型:(long)；<br/> 图片id<br/>；
	 * @return 返回结果说明. （List）图片信息集合
	 */
	public List getPhoto(long photo_id){
		List photo = new ArrayList();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			String sql="select id,photo_type_id,size_id,photo_name,photo_path,creator,create_time,status from t_photo with(nolock) where id=?";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			pstmt.setLong(1, photo_id);
			
			rs = pstmt.executeQuery();
			while(rs.next()){
				Map map = new HashMap();
				map.put("id", rs.getLong("id"));
				map.put("photo_type_id", rs.getLong("photo_type_id"));
				map.put("size_id", rs.getInt("size_id"));
				map.put("photo_name", rs.getString("photo_name"));
				map.put("photo_path", rs.getString("photo_path"));
				map.put("creator", rs.getLong("creator"));
				map.put("create_time", rs.getLong("create_time"));
				map.put("status", rs.getInt("status"));
				
				photo.add(map);
				
			}
		}catch(Exception sqle){
			sqle.printStackTrace();
		}finally{
			ConnectionManager.close(rs, pstmt, con);
		}
		
		
		return photo;
	}
	/**
	 * 函数说明：添加图片基本信息 <br/>
	 * 
	 * @param photo
	 *            类型:(Photo)；<br/> 图片对象<br/>；
	 * @return 返回结果说明. （boolean）是否操作成功，true为成功，false为失败
	 */
	public long addPhoto(Photo photo){
		long photoID=0;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String photoPath="";
		int index=photo.getPhoto_path().indexOf("?");
		if (index>0){
			photoPath=photo.getPhoto_path().substring(0, index);
		}else{
			photoPath=photo.getPhoto_path();
		}
		try{
			String sql="insert into t_photo(photo_type_id,size_id,photo_name,photo_path,creator,create_time,status)values" +
					   "(?,?,?,?,?,dbo.fn_datetime_to_timestamp(getdate()),1);SELECT SCOPE_IDENTITY() AS id";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, photo.getPhoto_type_id());
			pstmt.setInt(2, photo.getSize_id());
			pstmt.setString(3, photo.getPhoto_name());
			pstmt.setString(4,photoPath);
			pstmt.setLong(5,photo.getCreator());
			
		    rs=pstmt.executeQuery();
		    if (rs.next()){
		         photoID=rs.getLong("id");
		    }
		}catch(Exception sqle){
			sqle.printStackTrace();
			return 0;
		}finally{
			ConnectionManager.close(rs,pstmt, con);
		}
		return photoID;
		
	}
	/**
	 * 函数说明：删除图片基本信息 <br/>
	 * 
	 * @param photo
	 *            类型:(Photo)；<br/> 图片对象<br/>；
	 * @return 返回结果说明. （boolean）是否操作成功，true为成功，false为失败
	 */
	public boolean delPhoto(Photo photo){
		boolean flag=false;
		Connection con = null;
		PreparedStatement pstmt = null;
		try{
			String sql="update t_photo set status=3 where id=?";
			con = ConnectionManager.getDataSourceConnection(JNDI_NAME);
			pstmt = con.prepareStatement(sql);
			pstmt.setLong(1,photo.getId());
			
			
		    pstmt.execute();
		    flag=true;
		}catch(Exception sqle){
			sqle.printStackTrace();
			System.out.println("why---------------------:"+sqle.getMessage());
		}finally{
			ConnectionManager.close(pstmt, con);
		}
		return flag;
		
	}

}
